package com.x.erp.controller;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {

	@Autowired
	private FastFileStorageClient fdfsClient;// 文件服务

	/**
	 * 上传文件
	 *
	 * @param uploadFile
	 *            文件流
	 * @return
	 */
	@RequestMapping("/uploadFile")
	public ResultModel uploadFile(
			@RequestParam(value = "uploadFile", required = true) CommonsMultipartFile uploadFile) {
		if (uploadFile == null || uploadFile.isEmpty()) {
			return new ResultModel(ResultCode.RESULT_FAIL, "上传文件不能为空");
		}
		try {
			InputStream is = uploadFile.getInputStream();
			String pathUrl = fdfsClient.uploadFile(is, getFileExt(uploadFile.getOriginalFilename()));
			if (pathUrl == null || pathUrl.equalsIgnoreCase("null")) {
				return new ResultModel(ResultCode.RESULT_FAIL, "上传失败");
			}
			Map<String, Object> res = new HashMap<>();
			res.put("pathUrl", pathUrl);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "上传成功", res);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultModel(ResultCode.RESULT_FAIL, "上传失败");
		}
	}

	/**
	 * 上传图片
	 * 
	 * @param uploadFile
	 *            Base64编码
	 * @return
	 */
	@RequestMapping(value = "/uploadImage", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel uploadImage(String uploadFile, String extName) {
		if (StringUtils.isEmpty(uploadFile)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "图片不能为空！");
		}
		if (StringUtils.isEmpty(extName)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "图片格式不能为空！");
		}
		byte[] bytes = Base64.decodeBase64(uploadFile);
		try {
			String pathUrl = fdfsClient.uploadFile(bytes, extName);
			if (pathUrl == null || pathUrl.equalsIgnoreCase("null")) {
				return new ResultModel(ResultCode.RESULT_FAIL, "上传失败");
			}
			Map<String, Object> res = new HashMap<>();
			res.put("pathUrl", pathUrl);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "上传成功", res);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultModel(ResultCode.RESULT_FAIL, "上传失败！");
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param fileId
	 * @return
	 */
	@RequestMapping(value = "/deleteFile", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel deleteFile(String fileId) {
		if (StringUtils.isEmpty(fileId)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "fileId不能为空！");
		}
		try {
			fdfsClient.deleteFile(fileId);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultModel(ResultCode.RESULT_FAIL, "删除失败！");
		}
	}

	/**
	 * 获取文件后缀名（不带点）.
	 *
	 * @return 如："jpg" or "".
	 */
	public static String getFileExt(String fileName) {
		if (fileName == null || !fileName.contains(".")) {
			return "";
		} else {
			return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点
		}
	}
}
